//
//  XWHAHearingTestMarker.swift
//  XiaoweHearingAid
//
//  Created by Riven on 2023/7/25.
//

import UIKit
import Charts

class XWHAHearingTestMarker: MarkerImage {
    
    @objc open var color: UIColor
//    @objc open var arrowSize = CGSize(width: 15, height: 11)
    @objc open var font: UIFont
    @objc open var textColor: UIColor
    @objc open var insets: UIEdgeInsets
    @objc open var minimumSize = CGSize()
    
    fileprivate var label: String?
    fileprivate var _labelSize: CGSize = CGSize()
    fileprivate var _paragraphStyle: NSMutableParagraphStyle?
    fileprivate var _drawAttributes = [NSAttributedString.Key : Any]()
    
    @objc public init(color: UIColor, font: UIFont, textColor: UIColor, insets: UIEdgeInsets)
    {
        self.color = color
        self.font = font
        self.textColor = textColor
        self.insets = insets
        
        _paragraphStyle = NSParagraphStyle.default.mutableCopy() as? NSMutableParagraphStyle
        _paragraphStyle?.alignment = .center
        super.init()
    }
    
    open override func offsetForDrawing(atPoint point: CGPoint) -> CGPoint
    {
        var offset = self.offset
        var size = self.size

        if size.width == 0.0 && image != nil
        {
            size.width = image!.size.width
        }
        if size.height == 0.0 && image != nil
        {
            size.height = image!.size.height
        }

        let width = size.width
        let height = size.height
        let padding: CGFloat = 0.0

        var origin = point
        origin.x -= width / 2
        origin.y -= height

        if origin.x + offset.x < 0.0
        {
            offset.x = -origin.x + padding
        }
        else if let chart = chartView,
            origin.x + width + offset.x > chart.bounds.size.width
        {
            offset.x = chart.bounds.size.width - origin.x - width - padding
        }

        if origin.y + offset.y < 0
        {
            offset.y = height + padding;
        }
        else if let chart = chartView,
            origin.y + height + offset.y > chart.bounds.size.height
        {
            offset.y = chart.bounds.size.height - origin.y - height - padding
        }

        return offset
    }
    
    override func draw(context: CGContext, point: CGPoint)
    {
        guard let image = image else { return }

        let offset = offsetForDrawing(atPoint: point)
        
        var size = self.size
        
        if size.width == 0.0
        {
            size.width = image.size.width
        }
        if size.height == 0.0
        {
            size.height = image.size.height
        }
        
        var rect = CGRect(
            x: point.x + offset.x,
            y: point.y + offset.y,
            width: size.width,
            height: size.height)
        
        rect.origin.x -= size.width / 2.0
        rect.origin.y -= size.height
        
        let labelList = label?.components(separatedBy: "/n") ?? []
        var tLabelSize = CGSize.zero
        var lineHeight: CGFloat = 0
        
        for iLabel in labelList {
            let tSize = iLabel.size(withAttributes: _drawAttributes)
            lineHeight = tSize.height.ceil
            if tLabelSize.width < tSize.width {
                tLabelSize.width = tSize.width.ceil
            }
            tLabelSize.height += lineHeight
        }
//        _labelSize = label?.size(withAttributes: _drawAttributes) ?? CGSize.zero
        _labelSize = tLabelSize

        var labelRect = CGRect(center: rect.center, size: _labelSize)
        if labelList.count > 1 {
            labelRect.origin.y -= _labelSize.height * 0.25
        } else {
            labelRect.origin.y -= _labelSize.height / 3
        }
        UIGraphicsPushContext(context)
        image.draw(in: rect)
        
        for iLabel in labelList {
            iLabel.draw(in: labelRect, withAttributes: _drawAttributes)
            labelRect.origin.y += lineHeight
        }
//        label?.draw(in: labelRect, withAttributes: _drawAttributes)
        UIGraphicsPopContext()
    }
    
    @objc open func setLabel(_ newLabel: String)
    {
        label = newLabel
        
        _drawAttributes.removeAll()
        _drawAttributes[.font] = self.font
        _drawAttributes[.paragraphStyle] = _paragraphStyle
        _drawAttributes[.foregroundColor] = self.textColor
        
//        _labelSize = label?.size(withAttributes: _drawAttributes) ?? CGSize.zero
        
//        var size = CGSize()
////        size.width = _labelSize.width + self.insets.left + self.insets.right
////        size.height = _labelSize.height + self.insets.top + self.insets.bottom
//        size.width = _labelSize.width
//        size.height = _labelSize.height
//        size.width = max(minimumSize.width, size.width)
//        size.height = max(minimumSize.height, size.height)
//        self.size = size
    }

}
